home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / arc43.lbr / ARCRUN.MQC / arcrun.mac
Text File  |  1985-10-30  |  4KB  |  107 lines

  1. /*  ARC - Archive utility - ARCRUN
  2.  
  3. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  4. $define(version,Version $tag(
  5. TED_VERSION DB =1.13), created on $tag(
  6. TED_DATE DB =08/22/85) at $tag(
  7. TED_TIME DB =12:25:15))#
  8. $undefine(tag)#
  9.     $version
  10.  
  11. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  12.  
  13.     By:  Thom Henderson
  14.  
  15.     Description:
  16.          This file contains the routines used to "run" a file
  17.          which is stored in an archive.  At present, all we really do
  18.          is (a) extract a temporary file, (b) give its name as a system
  19.          command, and then (c) delete the file.
  20.  
  21.     Language:
  22.          Computer Innovations Optimizing C86
  23. */
  24. #include <stdio.h>
  25. #include "arc.h"
  26.  
  27. runarc(num,arg)                        /* run file from archive */
  28. int num;                               /* number of arguments */
  29. char *arg[];                           /* pointers to arguments */
  30. {
  31.     struct heads hdr;                  /* file header */
  32.     int run;                           /* true to run current file */
  33.     int did[$maxarg];                  /* true when argument was used */
  34.     int n;                             /* index */
  35.     char *makefnam();                  /* filename fixer */
  36.     FILE *fopen();                     /* file opener */
  37.  
  38.     for(n=0; n<num; n++)               /* for each argument */
  39.          did[n] = 0;                   /* reset usage flag */
  40.  
  41.     openarc(0);                        /* open archive for reading */
  42.  
  43.     if(num)                            /* if files were named */
  44.     {    while(readhdr(&hdr,arc))      /* while more files to check */
  45.          {    run = 0;                 /* reset run flag */
  46.               for(n=0; n<num; n++)     /* for each template given */
  47.               {    if(match(hdr.name,arg[n]))
  48.                    {    run = 1;       /* turn on run flag */
  49.                         did[n] = 1;    /* turn on usage flag */
  50.                         break;         /* stop looking */
  51.                    }
  52.               }
  53.  
  54.               if(run)                  /* if running this one */
  55.                    runfile(&hdr);      /* then do it */
  56.               else                     /* else just skip it */
  57.                    fseek(arc,hdr.size,1);
  58.          }
  59.     }
  60.  
  61.     else while(readhdr(&hdr,arc))      /* else run all files */
  62.          runfile(&hdr);
  63.  
  64.     closearc(0);                       /* close archive after changes */
  65.  
  66.     if(note)
  67.          for(n=0; n<num; n++)          /* report unused args */
  68.               if(!did[n])
  69.                    printf("File not found: %s\n",arg[n]);
  70. }
  71.  
  72. static runfile(hdr)                    /* run a file */
  73. struct heads *hdr;                     /* pointer to header data */
  74. {
  75.     FILE *tmp, *fopen();               /* temporary file */
  76.     char buf[$strlen], *makefnam();    /* temp file name, fixer */
  77.     char *dir, *gcdir();               /* directory stuff */
  78.  
  79.     makefnam("X",hdr->name,buf);       /* check out the name */
  80.     if(strcmp(buf,"X.BAT")
  81.     && strcmp(buf,"X.COM")
  82.     && strcmp(buf,"X.EXE"))
  83.     {    if(warn)
  84.               printf("File %s is not a .BAT, .COM, or .EXE\n",hdr->name);
  85.          fseek(arc,hdr->size,1);       /* skip this file */
  86.          return;
  87.     }
  88.  
  89.     makefnam("$ARCTEMP",hdr->name,buf);
  90.     if(warn)
  91.          if(tmp=fopen(buf,"wrb"))
  92.               abort("Temporary file %s already exists",buf);
  93.     if(!(tmp=fopen(makefnam("$ARCTEMP",hdr->name,buf),"wrb")))
  94.          abort("Unable to create temporary file %s",buf);
  95.  
  96.     if(note)
  97.          printf("Invoking file: %s\n",hdr->name);
  98.  
  99.     dir = gcdir("");                   /* see where we are */
  100.     unpack(arc,tmp,hdr);               /* unpack the entry */
  101.     fclose(tmp);                       /* release the file */
  102.     system("$ARCTEMP");                /* try to invoke it */
  103.     chdir(dir); free(dir);             /* return to whence we started */
  104.     if(unlink(buf) && warn)
  105.          printf("Cannot unsave temporary file %s\n",buf);
  106. }
  107.